package com.napier.unit3;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class GPSActivity extends Activity {
    
	private LocationManager lManager;
	private LocationListener lListener;
	private String locStr, prevLocStr;
	private TextView tv;
	private Location prevLoc;
	
	private TextView locationLat;
	private TextView locationLong;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        locationLat = (TextView)findViewById(R.id.locLat);
        locationLong = (TextView)findViewById(R.id.locLong);
        
        tv = new TextView(this);
        lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);    
        lListener = new MyLocationListener();
        lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lListener);
        
    }
    
	public class MyLocationListener implements LocationListener {
		
		public void onLocationChanged(Location location) {
			if (location != null) {
				double dLat = prevLoc.getLatitude() - location.getLatitude();
				double dLong = prevLoc.getLongitude() - location.getLongitude();
				
				locStr = "Current Location : lat = "+location.getLatitude()+" long = "+location.getLongitude();
				prevLocStr = "Previous Location : lat = "+prevLoc.getLatitude()+" long = "+prevLoc.getLongitude();
				Toast.makeText(GPSActivity.this, locStr, Toast.LENGTH_LONG).show();
				locationLat.setText("lat: "+dLat);
				locationLong.setText("long: "+dLong);
				tv.setText(locStr);
				setContentView(tv);
				prevLoc = lManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
				
			}
			
		}
	
		public void onProviderDisabled(String provider) {
			
		}
	
		public void onProviderEnabled(String provider) {
		}
	
		public void onStatusChanged(String provider, int status, Bundle extras) {
			
		}
		
	}
    
}